package DFA;

import org.omg.CORBA.INTERNAL;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class jiexiUtils {
    private static int maxWay;
    private int []initialValue = null;
    private HashMap<Integer,ArrayList<Integer>> finalArray;
    private int []initialState;
    private int []finalState;
    private List <String> list;   // 11| 12无空格模式
    private List <String> forList;// 1 1| 1 2


    public List<String> getList() {
        return list;
    }

    public int[] getInitialValue() {
        return initialValue;
    }

    public void setInitialValue(TxtReadIn readIn) {
        this.initialValue = readIn.getInitialValue();
    }

    public HashMap<Integer, ArrayList<Integer>> getFinalArray() {
        return finalArray;
    }



    public returnPOJO arrErweiGet(ArrayList show, int eventNumber, TxtReadIn readIn, int stateInHashmapNum
            ,ArrayList<Integer> dfa1Fianl,ArrayList<Integer> dfa2Fianl) {
        //扩展
        if (eventNumber <= 2)
            return null;
        initialState=readIn.getInitialValue();
        ArrayList<Integer> tempDFAFianl = new ArrayList();
        int events = show.size();
        int[][] arr = new int[events][3];
        int[][] result = transferShowToId(show,stateInHashmapNum,tempDFAFianl,dfa1Fianl,dfa2Fianl);
        for (int i = 0; i < events; i++) {
            showState shows = (showState) show.get(i);
//            xBuf start = shows.getStart();
//            boolean initializedStateSTART = start.getInitializedState();
//            xBuf end = shows.getEnd();
//            boolean initializedStateEND = end.getInitializedState();
            arr[i][0] = result[0][i];
            arr[i][2] = result[1][i];
            int way = shows.getWay();
            arr[i][1] = shows.getWay();
            //int max = (temp > c) ? temp : c;
            if (maxWay < way)
                maxWay = way;
        }

        return new returnPOJO(arr,tempDFAFianl);
    }

    public  int[][] transferShowToId(ArrayList show, int stateInHashmapNum,ArrayList tempDFAFianl,ArrayList<Integer> dfa1Fianl,ArrayList<Integer> dfa2Fianl) {
        int [][] result = new int[2][show.size()];
        this.list = new ArrayList<>();
        this.forList = new ArrayList<>();
        Integer startX1 ;
        Integer startX2 ;
        for (int i = 0; i < show.size(  ); i++) {
            showState shows = (showState) show.get(i);
            //startState
            xBuf start = shows.getStart();
            startX1= start.getX1();
            startX2= start.getX2();
            if ((startX1 == initialState[stateInHashmapNum] && startX2 == initialState [stateInHashmapNum+1])
            ||  (startX1 == initialState[stateInHashmapNum+1] && startX2 == initialState [stateInHashmapNum])) {
                start.setInitialized();
            }
            String s = ""+startX1;
            String forlist = s +" " + startX2;
            s = s + startX2;
            if (!list.contains(s)){
                list.add(s);
                forList.add(forlist);
            }
            forlist = "";
            s = "";
            //endState
            xBuf end = shows.getEnd();
            startX1= end.getX1();
            startX2= end.getX2();
            if ((startX1 == initialState[stateInHashmapNum] && startX2 == initialState [stateInHashmapNum+1])
                    ||  (startX1 == initialState[stateInHashmapNum+1] && startX2 == initialState [stateInHashmapNum])) {
                end.setInitialized();
            }
            s = ""+startX1;
            forlist = s +" " + startX2;
            s =s + startX2;
            if (!list.contains(s)){
                list.add(s);
                forList.add(forlist);
            }
            forlist = "";
            s = "";
        }
        for (int i = 0; i < show.size(); i++) {
            showState shows = (showState) show.get(i);
            for (int j = 1; j <= list.size(); j++) {
                String endString = shows.getEndString();
                String compare = list.get(j-1);
                if(compare.equals(endString))
                    result[1][i] = j;
            }
            String startString = shows.getStartString();
            for (int j = 1; j <= list.size(); j++) {
                String compare = list.get(j-1);
                if(compare.equals(startString))
                    result[0][i] = j;
            }
        }
        int j = 1;
        for (int k = 0; k < forList.size(); k++) {
            String[] split = forList.get(k).split(" ");
            int x11 = Integer.parseInt(split[0]);
            int x22 = Integer.parseInt(split[1]);
            if(dfa1Fianl.contains(x11) && dfa2Fianl.contains(x22))tempDFAFianl.add(j);
            j++;
        }
        //System.out.println(list);
        return result;
    }

    public static Integer getMaxWay() {
        return maxWay;
    }
}
